#!/usr/bin/env python
# -*- coding: UTF-8 -*-
# @Project:     rest_flask
# @File:        run.py
# @Author:      Fan GuiChuan
# @Date:        2025/6/26 13:39
# @Version:     Python3.7
# @Description: 框架入口文件
import json
import os
import sys

from utils.u_config import UConfig
from utils.u_path import UPath
from utils.u_json import UJson
from utils.u_log import ULog

if hasattr(sys, 'frozen'):
    ROOT_PATH = UPath.dir_name(sys.executable)
else:
    ROOT_PATH = UPath.dir_name(UPath.abspath(__file__))

os.environ['root_path'] = ROOT_PATH
CONFIG_FILE = UPath.path_join(ROOT_PATH, 'config/config.ini')

_config = UConfig.get(CONFIG_FILE)
PROJECT_CONFIG = UJson.field_decrypt(_config, "password")
LOG_DIR = UPath.make_sub_dir(ROOT_PATH, 'logs')

log_config = PROJECT_CONFIG.get('log')
log_config = UJson.fmt_str_value_to_int(log_config)
PROJECT_CONFIG['log'] = log_config
rotating_size_mb = log_config.get("rotating_size_mb", 20)
log_config['max_bytes'] = rotating_size_mb * 1024 * 1024
log_config['log_dir'] = LOG_DIR
default_log_name = log_config.get('log_name', 'app')

os.environ['LOG_CONFIG'] = json.dumps(log_config)

DEFAULT_LOGGER = ULog.get_logger(default_log_name, log_config)


if __name__ == '__main__':
    from app.flask_app import RestFlaskApp
    DEFAULT_LOGGER.debug(f"prj config: {PROJECT_CONFIG}")
    rest_flask_app = RestFlaskApp(DEFAULT_LOGGER, PROJECT_CONFIG)

    rest_flask_app.start()
